package com.bac.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.bac.bacplatform.BacApplication;
import com.bac.bacplatform.R;
import com.bac.http.HeaderStringRequest;
import com.baidu.android.bbalbs.common.a.c;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 工具类
 * 
 * @author Administrator
 * 
 */
public class Util {
	public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static int getMonthNum(String dateString){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cl =  Calendar.getInstance();
		try {
			Date date = format.parse(dateString);
			cl.setTime(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (cl.get(Calendar.MONTH)+1);
	}
	
	public static String getDayNum(String dateString){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cl =  Calendar.getInstance();
		try {
			Date date = format.parse(dateString);
			cl.setTime(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.valueOf(cl.get(Calendar.DATE));
	}
	
	public static String getHourNum(String dateString){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cl =  Calendar.getInstance();
		try {
			Date date = format.parse(dateString);
			cl.setTime(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.format("%02d", cl.get(Calendar.HOUR_OF_DAY));
	}

	public static String getMinuNum(String dateString){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cl =  Calendar.getInstance();
		try {
			Date date = format.parse(dateString);
			cl.setTime(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return String.format("%02d", cl.get(Calendar.MINUTE));
	}
	
	public static Dialog mProgressDialog = null;
	
	public static void showProgressDialog(Context context) {
		// 进度条
		RelativeLayout rl = (RelativeLayout) LayoutInflater.from(context)
				.inflate(R.layout.loading, null);
		mProgressDialog = new Dialog(context, R.style.dialog);
		mProgressDialog.getWindow().setContentView(rl);
		if (mProgressDialog != null && !mProgressDialog.isShowing()) {
			mProgressDialog.show();
		}
	}
	
	public static void dismissProgressDialog(){
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}

	static DisplayImageOptions options = Options.getListOptions();

	static final float DENSITY = 2.0f;

	/**
	 * 获取设备宽
	 * 
	 * @param context
	 * @return
	 */
	public static int getWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	/**
	 * 获取设备高
	 * 
	 * @param context
	 * @return
	 */
	public static int getHeight(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);

		return dm.heightPixels;
	}

	/**
	 * 获取屏幕密度
	 * 
	 * @param context
	 * @return
	 */
	public static float getDensity(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		return dm.density;
	}

	public static float getRATIO(Context context) {
		float density = getDensity(context);
		return density / DENSITY;
	}

	public static int getAdjustImageHeigh(Resources res, int resId,
			Context context) {
		// 首先不加载图片,仅获取图片尺寸
		final BitmapFactory.Options options = new BitmapFactory.Options();
		// 当inJustDecodeBounds设为true时,不会加载图片仅获取图片尺寸信息
		options.inJustDecodeBounds = true;
		// 此时仅会将图片信息会保存至options对象内,decode方法不会返回bitmap对象
		BitmapFactory.decodeResource(res, resId, options);
		float resWidth = options.outWidth;
		float resHeight = options.outHeight;
		float ratio = resWidth / resHeight;
		float screenWidth = getWidth(context);

		Log.i("Util---计算图片---", "width:" + resWidth + "/height:" + resHeight
				+ "/ratio:" + ratio + "/screenwidth:" + screenWidth);

		return (int) (screenWidth / ratio);
	}

	/**
	 * 获取设备的id
	 * 
	 * @param context
	 *            上下文
	 * @return 设备id
	 */
	public static String getDeviceID(Context context) {
		TelephonyManager tManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceId = tManager.getDeviceId();
		return deviceId;
	}

	/**
	 * 加载图片
	 * 
	 * @param path
	 *            图片地址
	 * @param imageLoader
	 *            ImageLoader imageLoader = ImageLoader.getInstance();
	 * @param imageView
	 *            需要加载的图片控件
	 */
	public static void setImageToPath(String path, ImageLoader imageLoader,
			ImageView imageView) {
		imageLoader.displayImage(path, imageView, options);
	}

	/**
	 * 网络请求GET
	 * 
	 * @param path
	 *            请求地址
	 * @param mQueue
	 *            请求队列
	 * @param l
	 *            成功回调
	 * @param el
	 *            失败回调
	 */
	public static void httpRequestToGet(String path, Listener<String> l,
			ErrorListener el) {
		// 网络请求
		HeaderStringRequest stringRequest = new HeaderStringRequest(path, l, el);
//		stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		BacApplication.getInstance().addToRequestQueue(stringRequest);
	}
	
	public static void httpRequestToGetWithParams(String url, Listener<String> l,
			ErrorListener el, final Map<String, String> map){
		String suffix = "?";
		if (map.isEmpty()) {
			suffix = "";
		}else{
			Set<String> keys =  map.keySet();
			for (String string : keys) {
				suffix += string + "=" + map.get(string) + "&&";
			}
		}
		if (suffix.endsWith("&&")) {
			suffix = suffix.substring(0, suffix.length() - 2);
		}
		String path = url + suffix;
		Log.i("net connection get with params----url", path);
		HeaderStringRequest stringRequest = new HeaderStringRequest(path, l, el);
		BacApplication.getInstance().addToRequestQueue(stringRequest);
	}

	/**
	 * @param url
	 *            接口地址
	 * @param l
	 *            成功回调
	 * @param el
	 *            失败回调
	 * @param map
	 *            post请求参数列表
	 */
	public static void httpStringPost(String url, Listener<String> l,
			ErrorListener el, final Map<String, String> map) {
		Log.i("net connection post----url", url);
		HeaderStringRequest stringRequest = new HeaderStringRequest(
				Method.POST, url, map, l, el);
		stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		BacApplication.getInstance().addToRequestQueue(stringRequest);
	}
}
